var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    publishid: '',
    array:['图片', '视频'],
    fileList:[{
      filename: '',
      temps:[]
    }],
    wxheight: wx.getSystemInfoSync().windowHeight,
    maxSize: 20971520, // 文件不能大于20M
    oclockid: '',
    alltemps: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      publishid: options.publishid,
      daylimit: options.daylimit,
      oclocktype: options.oclocktype
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      conheight: this.data.wxheight - 120
    })
  },

  //添加文件
  addFile(){
    const file = {
      filename: '',
      temps: []
    }
    var fileList = this.data.fileList
    fileList.push(file)
    this.setData({
      fileList: fileList
    })
  },

  delFile(){
    var fileList = this.data.fileList
    if (fileList.length > 1) {
      fileList.pop()
      this.setData({
        fileList: fileList
      })
    }
  },

  // 选择文件
  selectFile (e) {
    var fileList = this.data.fileList
    const index = e.currentTarget.dataset.index
    const that = this
    wx.chooseMedia({
      count: 6,
      mediaType: ['image', 'video'] 	,
      success (res) {
        var temps = fileList[index].temps
        const tempFiles = res.tempFiles
        const maxSize = that.data.maxSize
        for (var i = 0; i < tempFiles.length; i++) {
          const path = tempFiles[i].tempFilePath
          const size = tempFiles[i].size
          console.log(size)
          if(size > parseInt(maxSize)){
            wx.showToast({
              title: '文件过大',
              icon: 'error',
              duration: 2000
            })
            return
          }
          const fileType = tempFiles[i].fileType
          const temp = {
            path: path,
            size: size,
            fileType: fileType
          }
          temps.push(temp)
        }
        that.setData({
          fileList: fileList
        })
        that.caculateFile()
      }
    })
  },

  // 资料名称
  filenameinput (e) {
    const index = e.currentTarget.dataset.index
    var filename = e.detail.value.trim()
    var fileList = this.data.fileList
    fileList[index].filename = filename
    this.setData({
      fileList: fileList
    })

    var alltemps = [] // 提取文件
    // 判断资料名称是否重复
    for (var i = 0; i < fileList.length; i++) {
        if(index != i && (filename == fileList[i].filename)){
          wx.showToast({
            title: '已经存在',
            icon: 'error',
            duration: 2000
          })
          filename = ''
        }

        var temps = fileList[i].temps
        for (var j = 0; j < temps.length; j++) {
          var temp = temps[j]
          temp.filename = fileList[i].filename
          alltemps.push(temp)
        }
    }
    this.setData({
      alltemps: alltemps
    })
  },

  // 保存文件
  insertClockInWork () {
    const that = this
    const valid = this.validData()
    if (valid) {
      wx.showLoading({
        title: '提交中',
      })
      const usid = wx.getStorageSync('usid')
      const name = wx.getStorageSync('name')
      const publishid = this.data.publishid
      const daylimit = this.data.daylimit
      const oclocktype = this.data.oclocktype
      app.wxpost({
        url: 'issuedWorks/insertClockInWork.html',
        data: {
          usid: usid,
          name: name,
          publishid: publishid,
          daylimit: daylimit,
          oclocktype: oclocktype
        },
        success: function (res) {
          const oclockid = res.data.data.oclockid
          that.setData({
            oclockid: oclockid
          })
          that.uploadOclock(0)
        }
      })
    }
  },

  // 验证文件
  validData(){
    var fileList = this.data.fileList
    for (var i = 0; i < fileList.length; i++) {
      if (!fileList[i].filename) {
        wx.showToast({
          title: '请填写资源名称',
          icon: 'error',
          duration: 2000
        })
        return false
      }
      if (fileList[i].temps.length <= 0) {
        wx.showToast({
          title: '请选择文件',
          icon: 'error',
          duration: 2000
        })
        return false
      }
    }
    return true
  },


  // 上传文件
  uploadOclock(i) {
    const that = this
    var alltemps = this.data.alltemps
    const oclockid = this.data.oclockid
    const publishid = this.data.publishid
    app.wxupload({
      url:  'upload/uploadOclock.html',
      filePath: alltemps[i].path,
      name: 'file',
      formData: {
        oclockid: oclockid,
        filename: alltemps[i].filename,
        fileType: alltemps[i].fileType,
        size: alltemps[i].size,
        xh: i
      },
      success: function (res) {
        if (i < (alltemps.length-1)) {
          that.uploadOclock(i+1)
        }else{
          wx.hideLoading()
          wx.redirectTo({
            url: '../workDetail/index?publishid='+publishid,
          })
        }
      }
    })
  },

  // 阅览文件
  viewSource(e) {
    const index = e.currentTarget.dataset.index
    const tindex = e.currentTarget.dataset.tindex

    var current = 0
    var fileList = this.data.fileList
    for (var i = 0; i < fileList.length; i++) {
       if (i < index) {
          current = parseInt(current) + fileList[i].temps.length
       }else if (index == i) {
          current = parseInt(current) + parseInt(tindex)
       }
    }
    
    var alltemps = this.data.alltemps
    var resource = []
    for (var i = 0; i < alltemps.length; i++) {
      if (alltemps[i].path) {
        const source = {
          url: alltemps[i].path,
          type: alltemps[i].fileType
        }
        resource.push(source)
      }
    }
    wx.previewMedia({
      sources: resource,
      current: current
    })
  },

  // 删除临时文件
  delTempFile(e) {
    const index = e.target.dataset.index
    const tindex = e.target.dataset.tindex
    var fileList = this.data.fileList
    var temps = fileList[index].temps
    temps.splice(tindex, 1)
    this.setData({
      fileList: fileList
    })
    this.caculateFile()
  },

  caculateFile() {
    var alltemps = [] // 提取文件
    var fileList = this.data.fileList
    for (var i = 0; i < fileList.length; i++) {
      var temps = fileList[i].temps
      for (var j = 0; j < temps.length; j++) {
        var temp = temps[j]
        temp.filename = fileList[i].filename
        alltemps.push(temp)
      }
    }
    this.setData({
      alltemps: alltemps
    })
  }

})